package com.javaGather.day3;

import java.util.ArrayList;
import java.util.ListIterator;

public class Test3 {
    public static void main(String[] args) {


        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("C");
        arrayList.add("D");

        /**
         * 可以实现从头到尾 与 从尾到头遍历
         */
        ListIterator<String> stringListIterator = arrayList.listIterator();

        System.out.println("从头到尾");
//        System.out.println(stringListIterator.next()); //count++
//        System.out.println(stringListIterator.next());
//        System.out.println(stringListIterator.next()); //count = 3
        while (stringListIterator.hasNext()) {
            System.out.println(stringListIterator.next());
        }

        /**
         * 实现从尾到头，必须先从头到尾
         */
        System.out.println("从尾到头");
//        System.out.println(stringListIterator.previous()); // --count
//        System.out.println(stringListIterator.previous());
//        System.out.println(stringListIterator.previous());

        while (stringListIterator.hasPrevious()){
            System.out.println(stringListIterator.previous());
        }
    }


}
